package vue.FenetrePartie;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import modele.Position;
import modele.Batiment.CampMilitaire;
import modele.Batiment.Canon;
import modele.Batiment.ExtracteurElixir;
import modele.Batiment.ExtracteurOr;
import modele.Batiment.Laboratoire;
import modele.Batiment.Mortier;
import modele.Batiment.ReservoirElixir;
import modele.Batiment.ReservoirOr;
import modele.Batiment.TourArcher;
import modele.Batiment.TourSorcier;
import modele.Unite.Archer;
import modele.Unite.Geant;
import modele.Unite.Gobelin;
import modele.Unite.Soldat;

public class Magasin extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private Panneau p;
	private Map panneauMap;
	
	private JButton boutonRessources;
	private JButton boutonArmee;
	private JButton boutonDefenses;	
	private JButton boutonAnnuler;	
	
	private JButton boutonPrecedent;
	private JButton boutonExtracteurElixir;
	private JButton boutonReservoirElixir;
	private JButton boutonExtracteurOr;
	private JButton boutonReservoirOr;
	
	private JButton boutonCampMilitaire;
	private JButton boutonLaboratoire;
	
	private JButton boutonCanon;
	private JButton boutonTourArcher;
	private JButton boutonMortier;
	private JButton boutonTourSorcier;
	
	private JButton boutonSoldat;
	private JButton boutonArcher;
	private JButton boutonGobelin;
	private JButton boutonGeant;
	private modele.Batiment.Batiment batiment;

	/**
	 * 
	 * @param p, c'est le panneau principal qui contient les 4 panneaux de la partie.
	 * 
	 * Ce panneau est celui en bas à gauche, il va contenir l'ensemble des menus pour ajouter des batiments.
	 */
	public Magasin(Panneau p)
	{
		this.p = p;
		this.panneauMap = this.p.getPanneauMap();
		this.setLayout (null);
		this.setBackground(Color.GRAY);
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		
		addMenuMagasin();	
	}
	
	public void addMenuMagasin()
	{		
		this.deleteMenu();

		ImageIcon image = new ImageIcon("images/finies/boutonRessources.png"); 
		this.boutonRessources = new JButton(image);  
		this.boutonRessources.setFocusPainted(false);  
		this.boutonRessources.setContentAreaFilled(false); 
		this.boutonRessources.setBounds(50,15,102,102);
		this.add(this.boutonRessources);
				
		
		image = new ImageIcon("images/finies/boutonArmee.png"); 
		this.boutonArmee = new JButton(image);  
		this.boutonArmee.setFocusPainted(false);  
		this.boutonArmee.setContentAreaFilled(false); 
		this.boutonArmee.setBounds(200,15,102,102);
		this.add(this.boutonArmee);
		
		image = new ImageIcon("images/finies/boutonDefenses.png"); 
		this.boutonDefenses = new JButton(image);  
		this.boutonDefenses.setFocusPainted(false);  
		this.boutonDefenses.setContentAreaFilled(false); 
		this.boutonDefenses.setBounds(350,15,102,102);
		this.add(this.boutonDefenses);
		
		
		this.boutonRessources.addActionListener(this);
		this.boutonArmee.addActionListener(this);
		this.boutonDefenses.addActionListener(this);
	}
	
	
	public void addMenuRessources()
	{		
		this.deleteMenu();

		ImageIcon image = new ImageIcon("images/finies/flechePrecedent.png"); 
		this.boutonPrecedent = new JButton(image);  
		this.boutonPrecedent.setFocusPainted(false);  
		this.boutonPrecedent.setContentAreaFilled(false); 
		this.boutonPrecedent.setBounds(3,40,35,27);
		this.add(this.boutonPrecedent);
		
		image = new ImageIcon("images/finies/boutonExtracteurElixir.png"); 
		this.boutonExtracteurElixir = new JButton(image);  
		this.boutonExtracteurElixir.setFocusPainted(false);  
		this.boutonExtracteurElixir.setContentAreaFilled(false); 
		this.boutonExtracteurElixir.setBounds(40,15,75,100);
		this.add(this.boutonExtracteurElixir);
						
		image = new ImageIcon("images/finies/boutonReservoirElixir.png"); 
		this.boutonReservoirElixir = new JButton(image);  
		this.boutonReservoirElixir.setFocusPainted(false);  
		this.boutonReservoirElixir.setContentAreaFilled(false); 
		this.boutonReservoirElixir.setBounds(155,15,75,100);
		this.add(this.boutonReservoirElixir);
		
		image = new ImageIcon("images/finies/boutonExtracteurOr.png"); 
		this.boutonExtracteurOr = new JButton(image);  
		this.boutonExtracteurOr.setFocusPainted(false);  
		this.boutonExtracteurOr.setContentAreaFilled(false); 
		this.boutonExtracteurOr.setBounds(270,15,75,100);
		this.add(this.boutonExtracteurOr);
		
		image = new ImageIcon("images/finies/boutonReservoirOr.png"); 
		this.boutonReservoirOr = new JButton(image);  
		this.boutonReservoirOr.setFocusPainted(false);  
		this.boutonReservoirOr.setContentAreaFilled(false); 
		this.boutonReservoirOr.setBounds(385,15,75,100);
		this.add(this.boutonReservoirOr);
		
		image = new ImageIcon("images/finies/icone_supprimer.gif"); 
		this.boutonAnnuler = new JButton(image);  
		this.boutonAnnuler.setFocusPainted(false);  
		this.boutonAnnuler.setContentAreaFilled(false); 
		this.boutonAnnuler.setBounds(470,10,20,20);
		this.add(this.boutonAnnuler);		
		
		this.boutonPrecedent.addActionListener(this);
		this.boutonExtracteurElixir.addActionListener(this);
		this.boutonReservoirElixir.addActionListener(this);
		this.boutonExtracteurOr.addActionListener(this);
		this.boutonReservoirOr.addActionListener(this);
		this.boutonAnnuler.addActionListener(this);

	}
	
	
	public void addMenuArmee()
	{		
		this.deleteMenu();
		
		ImageIcon image = new ImageIcon("images/finies/flechePrecedent.png"); 
		this.boutonPrecedent = new JButton(image);  
		this.boutonPrecedent.setFocusPainted(false);  
		this.boutonPrecedent.setContentAreaFilled(false); 
		this.boutonPrecedent.setBounds(3,40,35,27);
		this.add(this.boutonPrecedent);
		
		
		image = new ImageIcon("images/finies/boutonCampMilitaire.png"); 
		this.boutonCampMilitaire = new JButton(image);  
		this.boutonCampMilitaire.setFocusPainted(false);  
		this.boutonCampMilitaire.setContentAreaFilled(false); 
		this.boutonCampMilitaire.setBounds(100,15,75,100);
		this.add(this.boutonCampMilitaire);
						
		image = new ImageIcon("images/finies/boutonLaboratoire.png"); 
		this.boutonLaboratoire = new JButton(image);  
		this.boutonLaboratoire.setFocusPainted(false);  
		this.boutonLaboratoire.setContentAreaFilled(false); 
		this.boutonLaboratoire.setBounds(275,15,75,100);
		this.add(this.boutonLaboratoire);
		
		image = new ImageIcon("images/finies/icone_supprimer.gif"); 
		this.boutonAnnuler = new JButton(image);  
		this.boutonAnnuler.setFocusPainted(false);  
		this.boutonAnnuler.setContentAreaFilled(false); 
		this.boutonAnnuler.setBounds(470,10,20,20);
		this.add(this.boutonAnnuler);	

		
		this.boutonPrecedent.addActionListener(this);
		this.boutonCampMilitaire.addActionListener(this);
		this.boutonLaboratoire.addActionListener(this);
		this.boutonAnnuler.addActionListener(this);

	}
	
	
	public void addMenuDefenses()
	{		
		
		this.deleteMenu();

		ImageIcon image = new ImageIcon("images/finies/flechePrecedent.png"); 
		this.boutonPrecedent = new JButton(image);  
		this.boutonPrecedent.setFocusPainted(false);  
		this.boutonPrecedent.setContentAreaFilled(false); 
		this.boutonPrecedent.setBounds(3,40,35,27);
		this.add(this.boutonPrecedent);
		
		image = new ImageIcon("images/finies/boutonCanon.png"); 
		this.boutonCanon = new JButton(image);  
		this.boutonCanon.setFocusPainted(false);  
		this.boutonCanon.setContentAreaFilled(false); 
		this.boutonCanon.setBounds(40,15,75,100);
		this.add(this.boutonCanon);
						
		image = new ImageIcon("images/finies/boutonTourArcher.png"); 
		this.boutonTourArcher = new JButton(image);  
		this.boutonTourArcher.setFocusPainted(false);  
		this.boutonTourArcher.setContentAreaFilled(false); 
		this.boutonTourArcher.setBounds(155,15,75,100);
		this.add(this.boutonTourArcher);
		
		image = new ImageIcon("images/finies/boutonMortier.png"); 
		this.boutonMortier = new JButton(image);  
		this.boutonMortier.setFocusPainted(false);  
		this.boutonMortier.setContentAreaFilled(false); 
		this.boutonMortier.setBounds(270,15,75,100);
		this.add(this.boutonMortier);
		
		image = new ImageIcon("images/finies/testRessources.png"); 
		this.boutonTourSorcier = new JButton(image);  
		this.boutonTourSorcier.setFocusPainted(false);  
		this.boutonTourSorcier.setContentAreaFilled(false); 
		this.boutonTourSorcier.setBounds(385,15,75,100);
		this.add(this.boutonTourSorcier);
		
		image = new ImageIcon("images/finies/icone_supprimer.gif"); 
		this.boutonAnnuler = new JButton(image);  
		this.boutonAnnuler.setFocusPainted(false);  
		this.boutonAnnuler.setContentAreaFilled(false); 
		this.boutonAnnuler.setBounds(470,10,20,20);
		this.add(this.boutonAnnuler);	
		
		this.boutonPrecedent.addActionListener(this);
		this.boutonCanon.addActionListener(this);
		this.boutonTourArcher.addActionListener(this);
		this.boutonMortier.addActionListener(this);
		this.boutonTourSorcier.addActionListener(this);
		this.boutonAnnuler.addActionListener(this);

	}
	
	public void addMenuUnite(modele.Batiment.Batiment batiment)
	{
		this.batiment = batiment;
		this.deleteMenu();
		
		ImageIcon image = new ImageIcon("images/finies/flechePrecedent.png"); 
		this.boutonPrecedent = new JButton(image);  
		this.boutonPrecedent.setFocusPainted(false);  
		this.boutonPrecedent.setContentAreaFilled(false); 
		this.boutonPrecedent.setBounds(3,40,35,27);
		this.add(this.boutonPrecedent);

		
		image = new ImageIcon("images/finies/boutonSoldat.png"); 
		this.boutonSoldat = new JButton(image);  
		this.boutonSoldat.setFocusPainted(false);  
		this.boutonSoldat.setContentAreaFilled(false); 
		this.boutonSoldat.setBounds(60,25,80,80);
		this.add(this.boutonSoldat);
		
		image = new ImageIcon("images/finies/boutonArcher.png"); 
		this.boutonArcher = new JButton(image);  
		this.boutonArcher.setFocusPainted(false);  
		this.boutonArcher.setContentAreaFilled(false); 
		this.boutonArcher.setBounds(160,25,80,80);
		this.add(this.boutonArcher);
		
		image = new ImageIcon("images/finies/boutonGobelin.png"); 
		this.boutonGobelin = new JButton(image);  
		this.boutonGobelin.setFocusPainted(false);  
		this.boutonGobelin.setContentAreaFilled(false); 
		this.boutonGobelin.setBounds(260,25,80,80);
		this.add(this.boutonGobelin);
		
		image = new ImageIcon("images/finies/boutonGeant.png"); 
		this.boutonGeant = new JButton(image);  
		this.boutonGeant.setFocusPainted(false);  
		this.boutonGeant.setContentAreaFilled(false); 
		this.boutonGeant.setBounds(360,25,80,80);
		this.add(this.boutonGeant);
		
		
		this.boutonPrecedent.addActionListener(this);
		this.boutonSoldat.addActionListener(this);
		this.boutonArcher.addActionListener(this);
		this.boutonGobelin.addActionListener(this);
		this.boutonGeant.addActionListener(this);

	}
	
	
	public void deleteMenu()
	{
		this.removeAll();
		this.repaint();
	}	
	
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		
		if(source == this.boutonRessources)
		{					
			addMenuRessources();
		}
		else if(source == this.boutonArmee)
		{
			addMenuArmee();	
		}
		else if(source == this.boutonDefenses)
		{
			addMenuDefenses();	
		}
		else if(source ==  this.boutonPrecedent)
		{
			addMenuMagasin();	
		}
		else if(source == this.boutonAnnuler)
		{
			this.panneauMap.supprimerBatimentAttente();
		}
		else if(source == this.boutonExtracteurElixir)
		{
			this.panneauMap.placerBatiment(new ExtracteurElixir(new Position(0,0)));
		}
		else if(source == this.boutonExtracteurOr)
		{
			this.panneauMap.placerBatiment(new ExtracteurOr(new Position(0,0)));
		}
		else if(source == this.boutonReservoirElixir)
		{
			this.panneauMap.placerBatiment(new ReservoirElixir(new Position(0,0)));
		}
		else if(source == this.boutonReservoirOr)
		{
			this.panneauMap.placerBatiment(new ReservoirOr(new Position(0,0)));
		}
		else if(source == this.boutonCampMilitaire)
		{
			this.panneauMap.placerBatiment(new CampMilitaire(new Position(0,0)));
		}
		else if(source == this.boutonLaboratoire)
		{
			this.panneauMap.placerBatiment(new Laboratoire(new Position(0,0)));
		}
		else if(source == this.boutonTourSorcier)
		{
			this.panneauMap.placerBatiment(new TourSorcier(new Position(0,0)));
		}
		else if(source == this.boutonMortier)
		{
			this.panneauMap.placerBatiment(new Mortier(new Position(0,0)));
		}
		else if(source == this.boutonCanon)
		{
			this.panneauMap.placerBatiment(new Canon(new Position(0,0)));
		}
		else if(source == this.boutonTourArcher)
		{
			this.panneauMap.placerBatiment(new TourArcher(new Position(0,0)));
		}
		else if(source == this.boutonSoldat)
		{
			this.p.getControleur().creerUniteAttaquante((CampMilitaire) this.batiment,new Soldat( ));
			this.p.rafraichir();
		}
		else if(source == this.boutonArcher)
		{
			this.p.getControleur().creerUniteAttaquante((CampMilitaire) this.batiment,new Archer());
			this.p.rafraichir();
		}
		else if(source == this.boutonGobelin)
		{
			this.p.getControleur().creerUniteAttaquante((CampMilitaire) this.batiment,new Gobelin());
			this.p.rafraichir();
		}
		else if(source == this.boutonGeant)
		{		
			this.p.getControleur().creerUniteAttaquante((CampMilitaire) this.batiment,new Geant());
			this.p.rafraichir();
		}
	
	}

	public void paintComponent(Graphics g) 
	{
		super.paintComponent(g);		
	}

}
